/*
 * Title - main menu, accumulation of the starfield and shiner objects
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import flash.events.TimerEvent;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.display.Sprite;
import flash.Lib;
import flash.net.SharedObject;
import flash.net.URLRequest;
import flash3f.BitFont;
import flash3f.InputHandler;
import flash3f.Images;
import lib3f.LCG;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.GlowFilter;
import flash.filters.BitmapFilterQuality;

class Title
{

	static public var nameheader : Control;
	static public var groupheader : Control;
	static public var seedheader : Control;
	static public var seedrandomize : Control;
	static public var done : Control;
	static public var nameentry : Control;
	static public var groupentry : Control;
	static public var seedentry : Control;

	static public var logo : Bitmap;
	static public var copyright : Bitmap;
	static public var container : Sprite;

	static public var seed : Int;
	static public var challengeseed : Int = -1;
	static public var seedselect : Int;
	static public var seedincrement : Int;	
	static public var settings : SharedObject;
	static public var mainmenu : Array<Control>;
	static public var soundtext : Control;
	
	static public var gamerequest : String;
	static public var stars : Starfield;

	static public function initgraphics()
	{
		stars = new Starfield();
		Lib.current.addChild(stars);
		// please turn off visible in starfield if i disable this in the future
			
		mainmenu = StandardControls.listTextButton(
		["Start Game","Name + Group",
		"Sound","Map Select","Visit Web Site"
		], 
		"Vera Bold", 18,
		0xDDDDDD,
		0x666688, 0.75,
		0xBBBBDD, 0.75,
		0xFFFFFF, 0.75,
		-200, 180, 12, 300);

		mainmenu[0].onClick = function()
		{
			StartGame.gamemode = 0;
			SFX.gotcastle = true;
			SFX.up(); // play the sound immediately rather than after the game begins
			StartGame.playrecording = false;
			Title.end();			
			Control.reset();
			GameFace.listening = true;
		}

		mainmenu[1].onClick = screenNames;

		soundtext = mainmenu[2];

		mainmenu[2].onClick = function ()
		{
				SFX.soundon = !SFX.soundon;
				SFX.towerplacement = true;
				
				if (settings!=null)
				{
					settings.setProperty("sound", SFX.soundon);
				}
				
				setsoundtext();
		}

		mainmenu[3].onClick = screenCustom;
		mainmenu[4].onClick = visitsite;
		
		nameheader = StandardControls.textButton(
		"Your name:",
		"Linux Libertine", 16,
		0xFFFFFF,
		0x886666, 0.75,
		0x886666, 0.75,
		0x221111, 0.75,
		-1800, 170, 3200);

		seedheader = StandardControls.textButton(
		"Enter a game number(0-999999)",
		"Linux Libertine", 16,
		0xFFFFFF,
		0x886666, 0.75,
		0x886666, 0.75,
		0x221111, 0.75,
		-1800, 170, 3200);

		groupheader = StandardControls.textButton(
		"Group name(optional, for sharing with friends):",
		"Linux Libertine", 16,
		0xFFFFFF,
		0x886666, 0.75,
		0x886666, 0.75,
		0x221111, 0.75,
		-200, 260, 3200);

		seedrandomize = StandardControls.textButton(
		"Randomize",
		"Vera Bold", 18,
		0xDDDDDD,
		0x666688, 0.75,
		0xBBBBDD, 0.75,
		0xFFFFFF, 0.75,
		680/2 - 150/2, 275, 150);		

		seedrandomize.onClick =	randomizeSeed;
		
		done = StandardControls.textButton(
		"Done",
		"Vera Bold", 18,
		0xDDDDDD,
		0x666688, 0.75,
		0xBBBBDD, 0.75,
		0xFFFFFF, 0.75,
		680/2 - 96/2, 500, 96);		

		done.onClick = function()
		{
			
			var testseed = Std.parseInt(seedentry.extra[0].text);
			if (testseed == null) {}
			else
			{
				if (testseed<=999999 && testseed>=0)
					seed = testseed;
			}
			screenMain();
			
		}
		
		nameentry = StandardControls.textEntryButton(
		Main.name, "Vera Bold", 32,  
		0xDDDDDD,
		0x666688, 0.75,
		0xBBBBDD, 0.75,
		0xFFFFFF, 0.75,
		340, 203,
		16, 'A-Z a-z 0-9 _+=\\-!@#$\\^%&*().,?',
		560
		);

		groupentry = StandardControls.textEntryButton(
		Main.group, "Vera Bold", 32, 
		0xDDDDDD,
		0x666688, 0.75,
		0xBBBBDD, 0.75,
		0xFFFFFF, 0.75,
		340, 293,
		16, 'A-Z a-z 0-9 _+=\\-!@#$\\^%&*().,?',
		560
		);

		seedentry = StandardControls.textEntryButton(
		Std.string(seed), "Vera Bold", 32, 
		0xDDDDDD,
		0x666688, 0.75,
		0xBBBBDD, 0.75,
		0xFFFFFF, 0.75,
		340, 203,
		16, '0-9',
		560
		);		
 		
		container = new Sprite();
		Lib.current.addChild(container);

		logo = Images.getbitmap(0xECECEC);
		logo.x = (680/2)-(logo.bitmapData.width/2);
		logo.y = 16;
		
		copyright = Images.getbitmap(0xCCCCCC);
		copyright.x = (680/2)-(copyright.bitmapData.width/2);
		copyright.y = logo.bitmapData.height;

		container.addChild(logo);
		container.addChild(copyright);
						
	}

	static public function screenMain()
	{
		Control.reset();
		setsoundtext();

		var odd = true;
		for (n in mainmenu)
		{
			n.show();
			if (odd)
			{
				n.setLeft(-300);
			}
			else
			{
				n.setRight(680+300);				
			}
			odd = (!odd);
			new ControlTween(n);
			n.tween.endx = 340-150;
			n.tween.transitionlength = 15;
			n.tween.transitiontype = "speeding";
		}
		
	}

	static public function screenCustom()
	{
		Control.reset();

		seedheader.show();
		
		seedentry.extra[0].text = Std.string(seed);
		seedentry.show();

		seedheader.show();
		new ControlTween(seedheader);
		seedheader.tween.startx = -1800;
		seedheader.tween.endx = 680/2 - 1600;
		seedheader.tween.transitionlength = 15;
		seedheader.tween.transitiontype = "speeding";
		
		seedrandomize.show();
		
		done.show();
		new ControlTween(done);
		done.tween.starty = 500;
		done.tween.endy = 425-32;
		done.tween.transitionlength = 15;
		done.tween.transitiontype = "slowing";		
	}

	static public function screenNames()
	{
		Control.reset();
		
		nameheader.show();		
		new ControlTween(nameheader);
		nameheader.tween.startx = -1800;
		nameheader.tween.endx = 680/2 - 1600;
		nameheader.tween.transitionlength = 15;
		nameheader.tween.transitiontype = "speeding";

		groupheader.show();
		new ControlTween(groupheader);
		groupheader.tween.startx = -200;
		groupheader.tween.endx = 680/2 - 1600;
		groupheader.tween.transitionlength = 15;
		groupheader.tween.transitiontype = "speeding";
		
		nameentry.show();
		groupentry.show();
		
		done.show();
		new ControlTween(done);
		done.tween.starty = 500;
		done.tween.endy = 425-32;
		done.tween.transitionlength = 15;
		done.tween.transitiontype = "slowing";		
	}
		
	static public function visitsite()
	{
		var urlreq = new URLRequest("http://spacecreeps.com");
		Lib.getURL(new URLRequest("http://spacecreeps.com"),"_blank");		
	}

	static public function setsoundtext()
	{
		if (SFX.soundon)
		{
			soundtext.extra[0].text = "Sound On";
		}
		else
		{
			soundtext.extra[0].text = "Sound Off";
		}	
	}
	
	static public function randomizeSeed()
	{
		SFX.wallplacement = true;
		seed = (LCG.randInt(999999));
		seedentry.extra[0].text = Std.string(seed);
	}

	static public function setname()
	{
		Main.name = nameentry.extra[0].text;
		Main.group = groupentry.extra[0].text;
		//Lib.current.removeChild(namelink);
		//Shiner.shiners.remove(namelink);
		/*namelink = new Shiner(Fonts.verdanaitalic.gen(["Change Name: "+Main.name+" ("+Main.group+")"]));
		namelink.x = 32;
		namelink.y = 425-54-(36*5);	*/
		
		if (settings!=null)
		{
			settings.setProperty("name", Main.name);
			settings.setProperty("group", Main.group);
		}

	}
	
	static public function end()
	{		
	
		for (n in 0...container.numChildren)
		{
			container.visible = false;
		}

		Shiner.clear();
		
		LCG.seed =  seed;
		ComponentManager.add(StartGame);
	}	

}
